package com.szyjjk.imageCrawler;

import com.szyjjk.controller.HardwareInterfaceController;
import com.szyjjk.controller.crawlerController;
import org.apache.http.HttpRequest;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @program: szyjjk
 * @description: 启动线程
 * @author: yangyue
 * @create: 2020-11-18 15:21
 */
public class StartUpTheard extends Thread {

    private int type;


    public StartUpTheard(int type) {
        this.type = type;
    }

    @Override
    public void run() {
        String httpPort = CrawlerTool.backWgetPath("server.port");
        String httpIp = getLocalIP();

        String donwLoadFlose = CrawlerTool.backWgetPath("donwLoadFlose");
       
        try {
    		//启动项目20分钟后执行
			Thread.sleep(20*60*1000);
			 //启动定时任务
	        // get("http://" + httpIp + ":" + httpPort + "/Watershed/autoToStart.do");
		} catch (Exception e) {
			// TODO: handle exception
		}
        if ("1".equals(donwLoadFlose)) {
            //启动下载
            if (type == 0) {
                while (true) {
                    try {
                        CrawlerTool.systemout("启动下载天气网站数据"," 下载");
                        get("http://" + httpIp + ":" + httpPort + "/crawler/beginCrawler.do");
                        Thread.sleep(24 * 60 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                try {
                    CrawlerTool.systemout("启动下载硬件网站数据"," 下载");
                    get("http://" + httpIp + ":" + httpPort + "/hardware/beginDownLoad.do");
                    Thread.sleep(24 * 60 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }


//        System.out.println("http://"+httpIp+":"+httpPort);

    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 获取IP
     * @Date 9:28 2020/11/23
     * @Param []
     **/
    public static String getLocalIP() {
        String localIP = "127.0.0.1";
        try {
            OK:
            for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if (address instanceof Inet4Address) {
                        localIP = address.getHostAddress();
                        break OK;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return localIP;
    }


    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 发送get请求
     * @Date 15:50 2020/11/20
     * @Param [url]
     **/
    public String get(String url) {
        //实例化httpclient  
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //实例化get方法  
        HttpGet httpget = new HttpGet(url);
        //请求结果  
        CloseableHttpResponse response = null;
        String content = "";
        try {
            //执行get方法  
            response = httpclient.execute(httpget);
            if (response.getStatusLine().getStatusCode() == 200) {
                content = EntityUtils.toString(response.getEntity(), "utf-8");
//                System.out.println(content);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
}